package com.bolehui.service.iSys.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bolehui.base.result.Result;
import com.bolehui.domain.dto.SysDicDTO;
import com.bolehui.domain.po.SysDictData;
import com.bolehui.mapper.SysDictDataMapper;
import com.bolehui.service.iSys.ISysDictDataService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author chendongjie     email:chendongjie@gz.iscas.ac.cn
 * @version 1.0
 * @ClassName SysDictDataServiceImpl
 * @Description
 * @createTime 2025/2/27-15:24
 * Copyright (C) 2021-2022 CASEEDER, All Rights Reserved.
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 根据父id查询下级数据
     * @param parentId
     * @return
     */
    @Override
    public Result getDictDataById(String parentId) {
        List<SysDictData> dictDataById = dictDataMapper.getDictDataById(parentId);
        if(CollectionUtils.isNotEmpty(dictDataById)){
            SysDicDTO result = new SysDicDTO();
            List<String> dictLabels = dictDataById.stream().map(SysDictData::getDictLabel).collect(Collectors.toList());
            List<Long> dictCodes = dictDataById.stream().map(SysDictData::getDictCode).collect(Collectors.toList());
            List<String> describes = dictDataById.stream().map(SysDictData::getLabelDescribe).collect(Collectors.toList());
            result.setDictLabels(dictLabels);
            result.setDictCodes(dictCodes);
            result.setDescribes(describes);
            return Result.success(result);
        }
        return Result.fail("");
    }

    @Override
    public Result getById(Long id) {
        SysDictData dicData = dictDataMapper.selectById(id);
        if(ObjectUtil.isNotNull(dicData)){
            SysDicDTO result = new SysDicDTO();
            result.setDictLabel(dicData.getDictLabel());
            return Result.success(result);
        }
        return Result.fail("");
    }
}
